package com.fbty.mt.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.dao.support.DaoSupport;

import com.fbty.base.dao.MtJdbcDao;
import com.fbty.base.domain.Page;
import com.fbty.mt.dao.AnnDao;
import com.fbty.mt.dao.AnswerDao;
import com.fbty.mt.dao.IndexPriceDao;
import com.fbty.mt.dao.NewsDao;
import com.fbty.mt.dao.ParmsDao;
import com.fbty.mt.dao.UserDao;
import com.fbty.mt.domain.Announcement;
import com.fbty.mt.domain.Answer;
import com.fbty.mt.domain.IndexPrice;
import com.fbty.mt.domain.News;
import com.fbty.mt.domain.Parms;
import com.fbty.mt.domain.User;
import com.fbty.mt.service.WelcomService;
import com.fbty.mt.utils.DegistUtil;

public class WelcomServiceImpl implements WelcomService {
	private AnswerDao answerDao;
	private ParmsDao parmsDao;
	private IndexPriceDao indexPriceDao;
	private NewsDao newsDao;
	private AnnDao annDao;
	private MtJdbcDao mtJdbcDao;
	private UserDao userDao;
	@Override
	public void saveAnswer(Answer answer) {
		answerDao.save(answer);
	}
	public AnswerDao getAnswerDao() {
		return answerDao;
	}
	public void setAnswerDao(AnswerDao answerDao) {
		this.answerDao = answerDao;
	}
	@Override
	public Page pageAnswer(Integer pageNo, Integer pageSize,Integer state) {
		String hql = "";
		if(state == 0) { //显示所有
			hql = "from Answer u order by id ";
			return answerDao.pagedQuery(hql, pageNo, pageSize);
		} else {
			hql = "from Answer u where u.state=? order by id";
			return answerDao.pagedQuery(hql, pageNo, pageSize, new Object[]{state});
		}
	}
	@Override
	public Page pageParms(Integer pageNo, Integer pageSize) {
		String hql = " from Parms";
		return parmsDao.pagedQuery(hql, pageNo, pageSize);
	}
	
	@Override
	public void saveParms(Integer id,String value) {
		Parms parms = parmsDao.get(id);
		parms.setValue(value);
		parmsDao.update(parms);
	}
	@Override
	public List<Parms> getList() {
		String hql = " from Parms";
		return parmsDao.find(hql);
	}
	@Override
	public void updateAnswer(Integer id) {
		Answer answer = answerDao.get(id);
		answer.setState(0);
		answerDao.update(answer);
	}
	@Override
	public void deleteAnswer(Integer id) {
		answerDao.removeById(id);
	}
	/**
	 * 前台价格设定
	 * @return
	 * @throws Exception
	 */
	public Page showPrice(Integer pageNo, Integer pageSize) {
		String hql = " from IndexPrice order by state,price";
		return indexPriceDao.pagedQuery(hql, pageNo, pageSize);
	}
	/**
	 * 前台新闻显示
	 * 只显示出最新的5条
	 * @return
	 * @throws Exception
	 */
	public List<News> showNews() {
		String hql = " from News where type=1 order by createdate ";
		List<News> list = newsDao.find(hql);
		return list.size()>5?list.subList(0, 5):list;
	}
	/**
	 * 前台新闻显示
	 * 只显示出最新的5条
	 * @return
	 * @throws Exception
	 */
	public List<Announcement> showAnns() {
		String hql = " from Announcement where type=1 order by createdate ";
		List<Announcement> list = newsDao.find(hql);
		return list.size()>5?list.subList(0, 5):list;
	}
	/**
	 * 首页查看公告或新闻
	 * @param id
	 * @param type 1公告 2新闻
	 * @return
	 */
	public Map<String, String> findById(Integer id,String type) {
		String sql = "";
		if("1".equals(type)) {
			sql = " select title,context from tab_ann where id="+id;
		} else if("2".equals(type)){
			sql = " select title,context from tab_news where id="+id;
		}
		List<Map<String, String>> list = mtJdbcDao.getJdbcTemplate().queryForList(sql);
		if(list.size() > 0) {
			return list.get(0);
		}
		return new HashMap<String, String>();
	}
	/**
	 * 首页查看更多公告或新闻
	 * @param id
	 * @param type 1公告 2新闻
	 * @return
	 */
	public Page findAll(Integer pageNo, Integer pageSize,String type) {
		String hql = "";
		if("1".equals(type)) {
			hql = " from Announcement ";
			return annDao.pagedQuery(hql, pageNo, pageSize);
		} else if("2".equals(type)){
			hql = " from News ";
			return newsDao.pagedQuery(hql, pageNo, pageSize);
		}
		return null;
	}
	public void updateIndexPrice(IndexPrice indexPrice) {
		indexPriceDao.update(indexPrice);
	}
	@Override
	public User findUserByEmail(String email) {
		String hql = " from User where email='"+email+"'";
		List<User> list = userDao.find(hql);
		return list.size()>0?list.get(0):null;
	}
	
	public ParmsDao getParmsDao() {
		return parmsDao;
	}
	public void setParmsDao(ParmsDao parmsDao) {
		this.parmsDao = parmsDao;
	}
	public IndexPriceDao getIndexPriceDao() {
		return indexPriceDao;
	}
	public void setIndexPriceDao(IndexPriceDao indexPriceDao) {
		this.indexPriceDao = indexPriceDao;
	}
	public NewsDao getNewsDao() {
		return newsDao;
	}
	public void setNewsDao(NewsDao newsDao) {
		this.newsDao = newsDao;
	}
	public AnnDao getAnnDao() {
		return annDao;
	}
	public void setAnnDao(AnnDao annDao) {
		this.annDao = annDao;
	}
	public MtJdbcDao getMtJdbcDao() {
		return mtJdbcDao;
	}
	public void setMtJdbcDao(MtJdbcDao mtJdbcDao) {
		this.mtJdbcDao = mtJdbcDao;
	}
	public UserDao getUserDao() {
		return userDao;
	}
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	@Override
	public void updatePwd(Integer userId,String password) {
		User user =userDao.get(userId);
		user.setType(0);//错误次数归零
		user.setPassword(DegistUtil.md5Digest(password));
		userDao.update(user);
	}

}
